package com.help.controller;


import com.help.common.api.CommonResult;
import com.help.common.util.JwtUtil;
import com.help.domain.HelpNote;
import com.help.domain.MsgBoard;
import com.help.domain.MsgVO;
import com.help.service.impl.MsgBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.*;

@Api(tags = "留言")
@Slf4j
@RestController
@RequestMapping("/msg")
public class MsgController {
    @Autowired
    private MsgBoardService msgBoardService;
    private String CACHE_NAME="data";

    private HashMap<String,Object> cache=new HashMap<>();


    @PostConstruct
    public void setCache(){
        cache.put(CACHE_NAME,getList());
    }

    @ApiOperation("发留言")
    @PostMapping("/release")
    public CommonResult myHelp(@RequestHeader("token") @ApiParam("用户token") String token,
                               @RequestParam("desc") @ApiParam("文字内容") String desc,
                               @RequestParam("img") @ApiParam("图片数组") String[] img){
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setAvatarUrl(JwtUtil.getAvatarUrl(token));
        msgBoard.setDesc(desc);
        msgBoard.setNickname(JwtUtil.getNickName(token));
        msgBoard.setOpenid(JwtUtil.getOpenid(token));
        msgBoard.setImgs(Arrays.toString(img));
        boolean save = msgBoardService.save(msgBoard);
        if (save){
            setCache();
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("所有留言")
    @GetMapping("/get")
    public CommonResult get(){
        List<MsgVO> data = (List<MsgVO>) cache.get(CACHE_NAME);
        return CommonResult.success(data);
    }

    @ApiOperation("我的留言")
    @GetMapping("/getMyself")
    public CommonResult getMyself(@RequestHeader("token") @ApiParam("用户token") String token) {
        String openid = JwtUtil.getOpenid(token);
        List<MsgBoard> list = msgBoardService.lambdaQuery().eq(MsgBoard::getOpenid, openid).list();
        List<MsgVO> data = new ArrayList<>();
        list.stream().sorted(Comparator.comparing(MsgBoard::getCreateTime).reversed()).forEach(msgBoard -> {
            MsgVO msgVO = new MsgVO();
            msgVO.setAvatarUrl(msgBoard.getAvatarUrl());
            msgVO.setDesc(msgBoard.getDesc());
            msgVO.setNickname(msgBoard.getNickname());
            String[] imgs=msgBoard.getImgs().substring(1,msgBoard.getImgs().length()-1).split(",");
            for (int i=0;i<imgs.length;i++){
                imgs[i]=imgs[i].trim();
            }
            msgVO.setImgs(imgs);
            msgVO.setId(msgBoard.getId());
            msgVO.setCreateTime(msgBoard.getCreateTime());
            data.add(msgVO);
        });
        return CommonResult.success(data);
    }


    public  List<MsgVO> getList(){
        List<MsgBoard> list = msgBoardService.list();
        List<MsgVO> data = new ArrayList<>();
        list.stream().sorted(Comparator.comparing(MsgBoard::getCreateTime).reversed()).forEach(msgBoard->{
            MsgVO msgVO = new MsgVO();
            msgVO.setAvatarUrl(msgBoard.getAvatarUrl());
            msgVO.setDesc(msgBoard.getDesc());
            msgVO.setNickname(msgBoard.getNickname());
            String[] imgs=msgBoard.getImgs().substring(1,msgBoard.getImgs().length()-1).split(",");
            for (int i=0;i<imgs.length;i++){
                imgs[i]=imgs[i].trim();
            }
            msgVO.setImgs(imgs);
            msgVO.setId(msgBoard.getId());
            msgVO.setCreateTime(msgBoard.getCreateTime());
            data.add(msgVO);
        });
        return data;
    }
}
